package cn.dinxian.web.controller.system;

import cn.dinxian.activiti.service.IActWorkflowFormDataService;
import cn.dinxian.common.core.controller.BaseController;
import cn.dinxian.common.core.page.TableDataInfo;
import cn.dinxian.entity.ActProcDTO;
import cn.dinxian.system.domain.SysPost;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeSet;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dingxian
 * @Title:
 * @param:
 * @return:
 * @Date 2023/5/3 23:26
 * @Description:
 */
@RestController
@RequestMapping("/system/Proc")
@Slf4j
public class SysProcController extends BaseController {
  
  private  List<String> da= Arrays.asList(new String[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"});
  @Autowired
  IActWorkflowFormDataService iActWorkflowFormDataService;
  
  @GetMapping("/list")
  public TableDataInfo list(SysPost post)
  {
    
    List<ActProcDTO> list = iActWorkflowFormDataService.getProc();
    List<ActProcDTO> result=new ArrayList<>();
    //ActProcDTO temp=new ActProcDTO();
    Map<String,String> mothlist=null;
    //按照月份来分组
    Map<String,List<ActProcDTO>> collect=list.stream().collect(Collectors.groupingBy(ActProcDTO::getType));
    //获取所有的月份
/*   list.stream().collect(Collectors.groupingBy(ActProcDTO::getMonth)).entrySet().forEach(
        entry -> {
          log.info("[MYLOG]:-----SysProcController/list==>entry.getKey():["+JSONObject.toJSONString(entry.getKey())+"]");
          //mothlist.put(entry.getKey(),"0");
          mothlist.put(entry.getKey(),"0");
        }
    );*/
    //(v1, v2) -> v1)
    mothlist = list.stream().collect(Collectors.toMap(ActProcDTO::getMonth,o->"0", (v1, v2) -> v1));
    
    Map<String, String> finalMothlist = mothlist;
    collect.entrySet().forEach( entry -> {
      ActProcDTO  temp=entry.getValue().get(0);
      Map<String,String> monthTemp= new HashMap<>(finalMothlist);
      //把时间数据填写到map中
      entry.getValue().stream().forEach(actProcDTO -> {monthTemp.put(actProcDTO.getMonth(), actProcDTO.getNum());});
      temp.setMonthNums( monthTemp.entrySet().stream().map(entrys -> entrys.getValue()).collect(Collectors.toList()));
      //log.info("[MYLOG]:-----SysProcController/list==>temp:["+JSONObject.toJSONString(temp)+"]");
      result.add(temp);
    });
    //log.info("[MYLOG]:-----SysProcController/list==>result:["+JSONObject.toJSONString(result)+"]");
    
    //获取全部的月份
    // Map<String, List<ActProcDTO>> collect1 = list.stream().collect(Collectors.toMap(ActProcDTO::getType,));
 /*   log.info("[MYLOG]:源数据:["+JSONObject.toJSONString(list)+"]");
    final ArrayList<ActProcDTO> collect1 = list.stream()
        .collect(Collectors.collectingAndThen(
            Collectors.toCollection(() -> new TreeSet<>(
            Comparator.comparing(o -> o.getMonth()))), ArrayList::new));
    log.info("[MYLOG]:操作后的数据:["+JSONObject.toJSONString(collect1)+"]");*/
    //collect.entrySet().forEach(
    //    entry -> {
    //      entry.getValue().stream().ma
    //      result.add()
    //    }
    //);
    result.get(0).setMonthNum(mothlist.entrySet().stream().map(
        entrys ->
        {
          //log.info("[MYLOG]:-----SysProcController/list==>entrys:["+JSONObject.toJSONString(entrys.getKey())+"]");
          return da.get(Integer.parseInt(entrys.getKey()));
        }
    ).collect(Collectors.toList()));
    return getDataTable(result);
  }

}
